package cn.jjzd.info.controller;

import cn.jjzd.common.pojo.PageResult;
import cn.jjzd.info.pojo.Consultant;
import cn.jjzd.info.request.ConsultantRequest;
import cn.jjzd.info.response.ArticlePageResponse;
import cn.jjzd.info.response.ConsultantPageResponse;
import cn.jjzd.info.response.ConsultantResponse;
import cn.jjzd.info.service.ConsultantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("consultant")
public class ConsultantController {

    @Autowired
    private ConsultantService consultantService;

    @GetMapping("list")
    public ResponseEntity<List<ConsultantResponse>> queryList(@RequestParam("areaId")Long areaId,@RequestParam("businessTypeId")Long businessTypeId){
        List<ConsultantResponse> list = consultantService.queryList(areaId,businessTypeId);
        if(list == null || CollectionUtils.isEmpty(list)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(list);
    }

    @GetMapping("page")
    public ResponseEntity<PageResult<ConsultantPageResponse>> queryArticlePage(
            @RequestParam(value = "key",required = false)String key,
            @RequestParam(value = "page",defaultValue = "1")Integer page,
            @RequestParam(value = "rows",defaultValue = "5")Integer rows,
            @RequestParam(value = "areaId",required = false)Long areaId,
            @RequestParam(value = "businessTypeId",required = false)Long businessTypeId
    ){
        PageResult<ConsultantPageResponse> result = consultantService.queryConsultantPage(key,page,rows,areaId,businessTypeId);
        if(result == null || CollectionUtils.isEmpty(result.getItems())){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(result);
    }

    @PostMapping
    public ResponseEntity<Void> saveConsultant(@RequestBody ConsultantRequest consultant){
        consultantService.saveConsultant(consultant);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PutMapping
    public ResponseEntity<Void> updateConsultant(@RequestBody ConsultantRequest consultant){
        consultantService.updateConsultant(consultant);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @DeleteMapping("delete")
    public ResponseEntity<Void> deleteById(@RequestBody ConsultantRequest consultant){
        consultantService.deleteById(consultant);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
}
